/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Block;

import DAO.AccountDAO;
import DAO.AccountLevelDAO;
import DAO.ConnectDatabase;
import DAO.DepartmentDAO;
import Helper.AccountHelper;
import Helper.AccountLevelHelper;
import Helper.DepartmentHelper;
import Model.AccountModel;
import Model.DepartmentModel;
import common.CalendarFormat;
import common.Constant;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author KIENLT
 */
public class GroupDoctors extends javax.swing.JPanel {

    ArrayList<AccountHelper> arrAccount;
    ArrayList<AccountHelper> arrAccountList;
    
    ArrayList<DepartmentHelper> arrDepartment;
    ArrayList<DepartmentHelper> arrDepartmentList;
    
    AccountHelper accountData;
    DepartmentHelper departmentData;
    ResultSet rs = null;
    
    private String strLevel;
    /**
     * Creates new form GroupDoctors
     */
    public GroupDoctors(String strLevel) {
        initComponents();
        this.strLevel = strLevel;
        changeLanguage();
//        fillAllAccount();
        loadComboboxAccountLevel();
        fillAllDepartment();
        
    }
    
    private void loadComboboxAccountLevel(){
        try {
            DefaultComboBoxModel cbxAccontLevel = new DefaultComboBoxModel();
            ResultSet rs = AccountLevelDAO.fillAll(ConnectDatabase.ConnectionDatabase());
            while(rs.next()){
                String strId = rs.getString("AccountLevelId");
                String strName = rs.getString("AccountLevelName");
                cbxAccontLevel.addElement(new AccountLevelHelper(strId, strName));
            }
            cbxLevel.setModel(cbxAccontLevel);
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(rs);
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex1);
            }
            ex.printStackTrace();
        }
    }
    
    private void deleteAccount() {
        // TODO add your handling code here:
        try {
            accountData = new AccountHelper();
            int[] rows = tblDoctor.getSelectedRows();
            for(int i=0; i<rows.length; i++){
                System.out.println(tblDoctor.getValueAt(rows[i], 0));
                String check = String.valueOf(tblDoctor.getValueAt(rows[i], 0));
                if(check.equals("true")){
                    accountData.setAccountId(String.valueOf(tblDoctor.getValueAt(rows[i], 1)));
                    AccountDAO.Delete(ConnectDatabase.ConnectionDatabase(), accountData);
                }
            }
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    // Hiển thị tất cả Account
    private void fillAllAccount() {
        try {
            DepartmentHelper departmentHelper = new DepartmentHelper();
            int row = tblDepartment.getSelectedRow();
            departmentHelper.setDepartmentID(tblDepartment.getValueAt(row, 0).toString());
            arrAccount = AccountDAO.FillAll(ConnectDatabase.ConnectionDatabase(), departmentHelper);
            arrAccountList = new ArrayList<AccountHelper>();
            
            for(AccountHelper acc : arrAccount){
                arrAccountList.add(acc);
            }
            
            tblDoctor.setModel(new AccountModel(arrAccountList));
            
            if(arrAccountList.size() > 0){
                tblDoctor.getSelectionModel().setSelectionInterval(0, 0);
                fillOneAccount();
            }
            
            tblDoctor.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    
                    boolean adjust = e.getValueIsAdjusting();
                    if(adjust){
                        fillOneAccount();
                    }
                    
                }
            });
            tblDoctor.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            tblDoctor.getColumnModel().getColumn(0).setPreferredWidth(25);
            tblDoctor.getColumnModel().getColumn(1).setPreferredWidth(50);
            tblDoctor.getColumnModel().getColumn(2).setPreferredWidth(120);
            tblDoctor.getColumnModel().getColumn(3).setPreferredWidth(80);
            tblDoctor.getColumnModel().getColumn(4).setPreferredWidth(150);
            tblDoctor.getColumnModel().getColumn(5).setPreferredWidth(130);
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }
    
    // Hiển thị tất cả Department
    private void fillAllDepartment(){
        try{
            arrDepartment = DepartmentDAO.fillAll(ConnectDatabase.ConnectionDatabase());
            arrDepartmentList = new ArrayList<DepartmentHelper>();
            
            for(DepartmentHelper item : arrDepartment){
                arrDepartmentList.add(item);
            }
            
            tblDepartment.setModel(new DepartmentModel(arrDepartmentList));
            
            if(arrDepartmentList.size() > 0){
                tblDepartment.getSelectionModel().setSelectionInterval(0, 0);
                fillAllAccount();
            }
            
            tblDepartment.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

                @Override
                public void valueChanged(ListSelectionEvent e) {
                    boolean adjust = e.getValueIsAdjusting();
                    if(adjust){
                        fillAllAccount();
                    }
                }
            });
            
            DefaultTableColumnModel model = (DefaultTableColumnModel) tblDepartment.getColumnModel();
            TableColumn tblCol = model.getColumn(1);
            tblCol.setPreferredWidth(250);
        }catch(Exception ex){
            try {
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex1);
            }
            ex.printStackTrace();
        }
    }
    
    // Hiển thị 1 acc trên form
    private void fillOneAccount() {
        try {
            accountData = new AccountHelper();
            int row = tblDoctor.getSelectedRow();
            accountData.setAccountId(tblDoctor.getValueAt(row, 1).toString());
            rs = AccountDAO.FillOne(ConnectDatabase.ConnectionDatabase(), accountData);
            if(rs.next()){
                txtAccountId.setText(rs.getString("AccountId"));
                txtAddress.setText(rs.getString("Address"));
                txtEmail.setText(rs.getString("Email"));
                txtFullName.setText(rs.getString("FullName"));
                txtPhone.setText(rs.getString("Phone"));
                txtUserName.setText(rs.getString("AccountName"));
                txtPassword.setText(rs.getString("AccountPass"));
                
                Calendar c = CalendarFormat.setCalendar(rs.getString("BirthDay"));
                dcBirthDay.setCalendar(c);
                
                int gender = rs.getString("Gender").equals("1") ? 1 : 0;
                cbxGender.setSelectedIndex(gender);
            }
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(rs);
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    
    
    private void changeLanguage() {
        if(Constant.getLanguage() == 0){
            Constant.initBundle();
            getLanguage();
        }else{
            Constant.initBundle();
            getLanguage();
        }
    }
    
    private void getLanguage() {
        lblAddress.setText(Constant.getBundle().getString("address"));
        lblBirthDay.setText(Constant.getBundle().getString("birthDay"));
        lblFullName.setText(Constant.getBundle().getString("fullName"));
        lblAccountID.setText(Constant.getBundle().getString("accountID"));
        lblPassword.setText(Constant.getBundle().getString("password"));
        lblPhone.setText(Constant.getBundle().getString("phone"));
        btnUpdate.setText(Constant.getBundle().getString("update"));
        btnDelete.setText(Constant.getBundle().getString("delete"));
        btnAdd.setText(Constant.getBundle().getString("add"));
        btnSearch.setText(Constant.getBundle().getString("search"));
        lblEmail.setText(Constant.getBundle().getString("email"));
        lblGender.setText(Constant.getBundle().getString("gender"));
        lblDepartment.setText(Constant.getBundle().getString("levelName"));
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblDepartment = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        lblAccountID = new javax.swing.JLabel();
        lblFullName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblBirthDay = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        txtAccountId = new javax.swing.JTextField();
        txtFullName = new javax.swing.JTextField();
        txtUserName = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        dcBirthDay = new com.toedter.calendar.JDateChooser();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        lblPhone = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblDoctor = new javax.swing.JTable();
        lblEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lblGender = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        cbxGender = new javax.swing.JComboBox();
        lblDepartment = new javax.swing.JLabel();
        cbxLevel = new javax.swing.JComboBox();

        setPreferredSize(new java.awt.Dimension(900, 459));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Department", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        tblDepartment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tblDepartment);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Account Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 13))); // NOI18N

        lblAccountID.setText("AccountID");

        lblFullName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblFullName.setText("Full Name");

        lblAddress.setText("Address");

        lblBirthDay.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblBirthDay.setText("BirthDay");

        lblPassword.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPassword.setText("Password");

        lblUserName.setText("UserName");

        txtAccountId.setEditable(false);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/img_x16/add2.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/img_x16/update.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/img_x16/delete2.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/img_x16/search.png"))); // NOI18N
        btnSearch.setText("Search");

        lblPhone.setText("Phone");

        tblDoctor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tblDoctor);

        lblEmail.setText("Email");

        lblGender.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblGender.setText("Gender");

        cbxGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));

        lblDepartment.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblDepartment.setText("Department");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(lblAccountID, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(txtAccountId, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(lblFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtFullName, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(lblBirthDay, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(dcBirthDay, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(lblPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(0, 96, Short.MAX_VALUE))))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(lblUserName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lblPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(lblAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(136, 136, 136)
                                        .addComponent(btnAdd)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnUpdate)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnDelete)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnSearch))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(lblEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblGender, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cbxGender, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(lblDepartment)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cbxLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 65, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAccountID)
                    .addComponent(txtAccountId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblFullName)
                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUserName)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPassword)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblPhone)
                        .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblBirthDay))
                    .addComponent(dcBirthDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAddress)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEmail)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblGender)
                    .addComponent(cbxGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDepartment)
                    .addComponent(cbxLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete)
                    .addComponent(btnSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        removeTableAccount();
    }

    private void removeTableAccount() {
        // TODO add your handling code here:
        int[] rows = tblDoctor.getSelectedRows();
        for(int i=rows.length-1; i>=0; i--){
            AccountModel accModel = (AccountModel)tblDoctor.getModel();
            if(String.valueOf(tblDoctor.getValueAt(rows[i], 0)).equals("true")){
                int option = JOptionPane.showConfirmDialog(this, "You are sure delete?", "Message", JOptionPane.YES_NO_OPTION);
                if(option == JOptionPane.YES_OPTION){
                    deleteAccount();
                    accModel.deleteRow(rows[i]);
                }
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        try {
            // TODO add your handling code here:
            AccountHelper account = new AccountHelper();
            int row = tblDepartment.getSelectedRow();
            account.setDepartmentId(tblDepartment.getValueAt(row, 0).toString());
            account.setFullName(txtFullName.getText());
            account.setUserName(txtUserName.getText());
            account.setPassword(txtPassword.getText());
            account.setAddress(txtAddress.getText());
            account.setDatetime(CalendarFormat.getCalendar(dcBirthDay));
            account.setEmail(txtUserName.getText());
            account.setGender(String.valueOf(cbxGender.getSelectedIndex()));
            account.setPhone(txtPhone.getText());
            account.setAccountLevelId(((AccountLevelHelper)cbxLevel.getSelectedItem()).getAccountLevelId());
            
            if(!AccountDAO.checkUserName(ConnectDatabase.ConnectionDatabase(), account)){
                int query = AccountDAO.Insert(ConnectDatabase.ConnectionDatabase(), account);
                if(query > 0)
                {
                    JOptionPane.showMessageDialog(this, "Insert Success.", "Message", JOptionPane.INFORMATION_MESSAGE);
                    fillAllAccount();
                }
                else
                {
                    JOptionPane.showMessageDialog(this, "Insert Fail.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }else{
                JOptionPane.showMessageDialog(this, "UserName is exists.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        try {
            // TODO add your handling code here:
            AccountHelper acc = new AccountHelper();
            acc.setAccountId(txtAccountId.getText());
            acc.setAddress(txtAddress.getText());
            acc.setUserName(txtUserName.getText());
            acc.setPassword(txtPassword.getText());
            acc.setDatetime(CalendarFormat.getCalendar(dcBirthDay));
            acc.setEmail(txtEmail.getText());
            acc.setFullName(txtFullName.getText());
            acc.setGender(String.valueOf(cbxGender.getSelectedIndex()));
            acc.setPhone(txtPhone.getText());
            int query = AccountDAO.UpdateAccount(ConnectDatabase.ConnectionDatabase(), acc);
            if(query > 0){
                JOptionPane.showMessageDialog(this, "Update Success.", "Message", JOptionPane.INFORMATION_MESSAGE);
                fillAllAccount();
            }else{
                JOptionPane.showMessageDialog(this, "Update Fail.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdate;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbxGender;
    private javax.swing.JComboBox cbxLevel;
    private com.toedter.calendar.JDateChooser dcBirthDay;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAccountID;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblBirthDay;
    private javax.swing.JLabel lblDepartment;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblFullName;
    private javax.swing.JLabel lblGender;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JTable tblDepartment;
    private javax.swing.JTable tblDoctor;
    private javax.swing.JTextField txtAccountId;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
